package com.yx.cloud.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.iot.product.sweeper.bean.CameraSettings;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanSettingParam;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.AiAvoidanceActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * AI避障
 */
public class AIAvoidanceActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = "AIAvoidanceActivity";
    private AiAvoidanceActivityBinding binding;
    private Robot robot;
    private CameraSettings cameraSettings;

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = AiAvoidanceActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    @SuppressLint("LongLogTag")
    private void initData() {
        robot = (Robot) AppContext.get().getCurrentDevice();
        cameraSettings = robot.getCameraSettings();

        if (cameraSettings.isAiAvoidanceSwitch()) {
            binding.buttonAIAvoidance.setBackgroundResource(R.mipmap.switch_track_on);
        } else {
            binding.buttonAIAvoidance.setBackgroundResource(R.mipmap.switch_track_off);
        }
        binding.ivCameraAiobstacleReturn.setOnClickListener(this);
        binding.buttonAIAvoidance.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_cameraAiobstacleReturn) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.button_AIAvoidance) {

            if (cameraSettings.isAiAvoidanceSwitch()) {
                aIThreeHintDialog(getString(R.string.MV_Video_AI_Close_Title), getString(R.string.off_AI_Three_content));
            } else {
             //   binding.buttonAIAvoidance.setBackgroundResource(R.mipmap.switch_track_on);
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setAiAvoidanceSwitch(true).build());
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MESSAGE_TYPE_CAMERASETTING:
                if (cameraSettings.isAiAvoidanceSwitch()) {
                    binding.buttonAIAvoidance.setBackgroundResource(R.mipmap.switch_track_on);
                } else {
                    binding.buttonAIAvoidance.setBackgroundResource(R.mipmap.switch_track_off);
                }
                break;
        }
    }

    @SuppressLint("LongLogTag")
    private void aIThreeHintDialog(String dialogTitle, String content) {
        Dialog dialog = new Dialog(AIAvoidanceActivity.this);
        dialog.setContentView(R.layout.outlogin_dialog);
        dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        dialog.setCanceledOnTouchOutside(false);
        TextView mBtnOutLoginConfirm = dialog.findViewById(R.id.btn_outLoginConfirm);
        TextView mBtnOutLoginCancel = dialog.findViewById(R.id.btn_outLoginCancel);
        TextView mTvDialogTitle = dialog.findViewById(R.id.tv_dialogTitle);
        TextView mTvContent = dialog.findViewById(R.id.tv_content);
        mTvDialogTitle.setText(dialogTitle);
        mTvContent.setText(content);

        mBtnOutLoginConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "11111111111111111111111111111: ");
               // binding.buttonAIAvoidance.setBackgroundResource(R.mipmap.switch_track_off);
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setAirCleanerSwitch(false).build());
                AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setHomeEnvironmentRecognitionSwitch(false).build());//家居开关
                dialog.dismiss();
            }
        });
        mBtnOutLoginCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "222222222222222222222222222: ");
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
